package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrder;
import com.shrct.businessprocess.purchase.RCBasePurchasePayRequest;
import com.shrct.businessprocess.purchase.RCPurchaseOrderAcceptPay;
import com.shrct.businessprocess.purchase.RCPurchaseOrderPayProcess;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseOrderImpl;
import com.shrct.businessprocess.purchase.impl.RCPayRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCBasePurchasePayRequestProxy;
import edu.fudan.langlab.domain.workflow.BusinessProcess;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCBASEPURCHASEPAYREQUEST_TYPE")
public class RCBasePurchasePayRequestImpl extends RCPayRequestImpl implements RCBasePurchasePayRequest {
  public RCBasePurchasePayRequestImpl() {
    super();
  }
  
  public RCBasePurchasePayRequestImpl(final RCBasePurchaseOrder purchaseOrder) {
    super();
    this.purchaseOrder = purchaseOrder;
  }
  
  @JoinColumn(name = "purchaseOrder_id")
  @ManyToOne(targetEntity = RCBasePurchaseOrderImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCBasePurchaseOrder purchaseOrder;
  
  public RCBasePurchaseOrder getPurchaseOrder() {
    return this.purchaseOrder;
  }
  
  public RCBasePurchasePayRequest setPurchaseOrder(final RCBasePurchaseOrder purchaseOrder) {
    this.purchaseOrder = purchaseOrder;
    return this;			
    
  }
  
  public RCExchangeRate getDefaultExchangeRate() {
    RCExchangeRate _defaultExchangeRate = this.purchaseOrder.getDefaultExchangeRate();
    return _defaultExchangeRate;
  }
  
  public RCBudget getBudget() {
    RCBudget _budget = this.purchaseOrder.getBudget();
    return _budget;
  }
  
  public String getEntityName() {
    String _entityName = this.purchaseOrder.getEntityName();
    String _plus = ("\u8BA2\u5355\u53F7:" + _entityName);
    return _plus;
  }
  
  public BusinessProcess getProcess() {
    IGenericQuery<RCPurchaseOrderPayProcess> query=getObjectFactory().createGenericQuery(RCPurchaseOrderPayProcess.class,"select e from com.shrct.businessprocess.purchase.impl.RCPurchaseOrderPayProcessImpl as e   where ((e.payRequest) = (:this)) and  e.active = 1      ").setParameter("this",this);
    RCPurchaseOrderPayProcess _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public Collection<RCPurchaseOrderAcceptPay> getPays() {
    IGenericQuery<RCPurchaseOrderAcceptPay> query=getObjectFactory().createGenericQuery(RCPurchaseOrderAcceptPay.class,"select pay from com.shrct.businessprocess.purchase.impl.RCPurchaseOrderAcceptPayImpl as pay   where ((pay.pay) = (:this)) and  pay.active = 1      ").setParameter("this",this);
    Collection<RCPurchaseOrderAcceptPay> _list = query.list();
    return _list;
  }
  
  /**
   * 已分配给收货的金额
   */
  public RCProjectMoney getUsedAmount() {
    Collection<RCPurchaseOrderAcceptPay> _pays = this.getPays();
    final Function1<RCPurchaseOrderAcceptPay,RCProjectMoney> _function = new Function1<RCPurchaseOrderAcceptPay,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPurchaseOrderAcceptPay it) {
          RCProjectMoney _amount = it.getAmount();
          return _amount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCPurchaseOrderAcceptPay, RCProjectMoney>map(_pays, _function);
    RCExchangeRate _defaultExchangeRate = this.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  /**
   * 还未分配给收货的金额
   */
  public RCProjectMoney getRemainAmount() {
    RCProjectMoney _totalPaidProjectAmount = this.getTotalPaidProjectAmount();
    RCProjectMoney _usedAmount = this.getUsedAmount();
    RCProjectMoney _substract = _totalPaidProjectAmount.substract(_usedAmount);
    return _substract;
  }
  
  public static RCBasePurchasePayRequest create(final RCBasePurchaseOrder purchaseOrder, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCBasePurchasePayRequest rCBasePurchasePayRequest = new com.shrct.businessprocess.purchase.impl.RCBasePurchasePayRequestImpl(
    	purchaseOrder
    );
    objectFactory.create(rCBasePurchasePayRequest);
    return rCBasePurchasePayRequest;			
    
  }
  
  public static RCBasePurchasePayRequest createRCBasePurchasePayRequestImpl(final RCBasePurchaseOrder purchaseOrder, final IObjectFactory objectFactory, final Procedure1<RCBasePurchasePayRequest> init) {
    com.shrct.businessprocess.purchase.RCBasePurchasePayRequest rCBasePurchasePayRequest = new com.shrct.businessprocess.purchase.impl.RCBasePurchasePayRequestImpl(
    	purchaseOrder
    );
    objectFactory.create(rCBasePurchasePayRequest);
    init.apply(rCBasePurchasePayRequest);
    
    return rCBasePurchasePayRequest;			
    
  }
  
  public RCBasePurchasePayRequestProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCBasePurchasePayRequestProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCBasePurchasePayRequestProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
